<template>
  <div style="height: 100%" ref="echart">echart</div>
</template>

<script>
import echarts from 'echarts'
export default {
  props: {
    chartData: {
      type: Object,
      default() {
        return {
          xData: [],
          series: [],
        }
      },
    },
    isAxisChart: {
      // 是否需要显示坐标做
      type: Boolean,
      default: true,
    },
  },
  computed: {
    options() {
      return this.isAxisChart ? this.axisOption : this.normalOption
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse
    }
  },
  watch: {
    // 监听chartData是否发生改变
    chartData: {
      handler: function () {
        this.initChart()
      },
      deep: true,
    },
    isCollapse() {
      setTimeout(() => {
        this.resizeChart()
      }, 300)
    }
  },
  data() {
    return {
      echart: null,
      axisOption: {
        legend: {
          textStyle: {
            color: '#333', // 配置文字描述的字体颜色
          },
        },
        xAxis: {
          type: 'category',
          data: [],
          axisLine: {
            lineStyle: { // 更改X轴线的颜色
              color: '#17b3a3'
            }
          },
          axisLabel: { // 如果不加这个样式，x轴的标签也是#17b3a3颜色
            color: '#333'
          }
        },
        yAxis: [
          {
            type: 'value',
            axisLine: { // 更改Y轴线的颜色
              lineStyle: {
                color: '#17b3a3'
              }
            },
          },
        ],
        color: [
          '#2ec7c9',
          '#b6a2de',
          '#5ab1ef',
          '#ffb980',
          '#d87a80',
          '#8d98b3',
          '#e5cf0d',
          '#97b552',
          '#95706d',
          '#dc69aa',
          '#07a2a4',
          '#9a7fd1',
          '#588dd5',
          '#f5994e',
          '#c05050',
          '#59678c',
          '#c9ab00',
          '#7eb00a',
          '#6f5553',
          '#c14089'
        ],
        tooltip: { // 点到x坐标上触发对比效果
          trigger: 'axis'
        },
        grid: { // 绘制折线图的偏移，默认10%
          left: '20%'
        },
      },
      normalOption: {
        // 配置饼图的颜色和点击效果
        tooltip: {
          trigger: 'item'
        },
        color: ['#0f78f4', '#dd536b', '#9462e5', '#a6a6a6', '#e1bb22', '#39c362', '#3ed1cf'],
        series: [],
      },
    }
  },
  methods: {
    initChart() {
      // 初始化echarts
      this.initChartData()
      if (this.echart) {
        this.echart.setOption(this.options)
      } else {
        this.echart = echarts.init(this.$refs.echart)
        this.echart.setOption(this.options)
      }
    },
    initChartData() {
      // 设置图表的data
      if (this.isAxisChart) {
        this.axisOption.xAxis.data = this.chartData.xData
        this.axisOption.series = this.chartData.series
      } else {
        this.normalOption.series = this.chartData.series
      }
    },
    // 图表是否发生变化
    resizeChart() {
      this.echart ? this.echart.resize() : ''
    }
  },
  // 监听Windows的变化事件，当发生变化echart则重新绘制
  mounted() {
    window.addEventListener('resize', this.resizeChart)
  },
  // 在Windows销毁的时候也销毁组件，防止内存泄露
  destroyed() {
    window.removeEventListener('resize', this.resizeChart)
  }
}
</script>
